﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DriveBeamEditor.Controls.General
{
    [Designer(typeof(CollapsibleBoxDesigner))]
    public partial class CollapsibleBox : UserControl
    {
        #region Fields

        private bool _isCollapsed = true;

        public event EventHandler Collapsed;
        public event EventHandler Expanded;

        #endregion

        #region Properties

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel DropZone
        {
            get { return dropZone; }
        }

        public bool IsCollapsed
        {
            get { return _isCollapsed; }
            set
            {
                _isCollapsed = value;
                if (value) Collapse();
                else Expand();
            }
        }

        public override Size MinimumSize
        {
            get { return base.MinimumSize; }
            set
            {
                base.MinimumSize = value;
                if (_isCollapsed) Collapse();
            }
        }

        public override Size MaximumSize
        {
            get { return base.MaximumSize; }
            set
            {
                base.MaximumSize = value;
                if (!_isCollapsed) Expand();
            }
        }

        public Theme ButtonsTheme
        {
            get { return xbCollapse.Theme; }
            set { SetButtonsTheme(value); }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get { return box.Text; }
            set { box.Text = value; }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                base.ForeColor = value;
                box.ForeColor = value;
            }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Font Font
        {
            get { return base.Font; }
            set
            {
                base.Font = value;
                box.Font = value;
            }
        }

        #endregion

        #region CTORs

        public CollapsibleBox()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void xbCollapse_Click(object sender, EventArgs e)
        {
            if (_isCollapsed) Expand();
            else Collapse();
        }

        #endregion

        #region Methods

        public void Collapse()
        {
            _isCollapsed = true;
            xbCollapse.Text = @"+";
            Size = MinimumSize;

            if (Collapsed != null)
                Collapsed(this, new EventArgs());
        }

        public void Expand()
        {
            _isCollapsed = false;
            xbCollapse.Text = @"-";
            Size = MaximumSize;

            if (Expanded != null)
                Expanded(this, new EventArgs());
        }

        private void SetButtonsTheme(Theme theme)
        {
            xbCollapse.Theme = theme;
            xbCollapse.Invalidate();
        }

        #endregion
    }
}
